package com.iseastar.guojiang.station;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.guojiang.app.AppCaptureActivity;
import com.iseastar.guojiang.model.StationParcelBean;
import com.shouhuobao.station.R;

import java.util.ArrayList;
import java.util.HashSet;

import droid.frame.activity.base.BaseAdapterWithImage;
import droid.frame.utils.lang.Str;

public class StationParcelListAdapter extends BaseAdapterWithImage<StationParcelBean> {
    /**
     * 未支付总数
     */
    private int unPayCount;
    /**
     * 已支付数量
     */
    private int payedCount;

    public int getUnPayCount() {
        return unPayCount;
    }

    public void setUnPayCount(int unPayCount) {
        this.unPayCount = unPayCount;
    }

    public int getPayedCount() {
        return payedCount;
    }

    public void setPayedCount(int payedCount) {
        this.payedCount = payedCount;
    }

    /**
     * 选中的item的id
     */
    private HashSet<Integer> checkedIds;

    public StationParcelListAdapter(ArrayList<StationParcelBean> items, Activity context, ViewGroup listView) {
        super(items, context, listView);
        checkedIds = new HashSet<Integer>();
        if (items == null) {
            items = new ArrayList<StationParcelBean>();
        }
        // items.clear();
        // for (int i = 0; i < 6; i++) {
        // OrderBean item = new OrderBean();
        // item.setOrderId(i + 1);
        // items.add(item);
        // // 默认全部选中
        // checkedIds.add(item.getId());
        // }
        super.items = items;
    }

    /**
     * 如果当前item已经选中 , 则取消选中
     *
     * @param id
     */
    public void setChecked(int id) {
        if (checkedIds.contains(id)) {
            checkedIds.remove(id);
        } else {
            checkedIds.add(id);
        }
        notifyDataSetChanged();
    }

    /**
     * 选中|取消全部
     */
    public void checkAlls(boolean isChecked) {
        if (items == null) {
            return;
        }
        if (isChecked == false) {
            checkedIds.clear();
        } else {
            for (int i = 0; i < items.size(); i++) {
                StationParcelBean item = getItem(i);
                checkedIds.add(item.getParcelId());
            }
        }
        // update
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.station_check_item, parent, false);
            holder.goodsImg = (ImageView) convertView.findViewById(R.id.station_parcel_goodsImage);
            holder.bagCode = (TextView) convertView.findViewById(R.id.station_parcel_bagCode);
            holder.goodsType = (TextView) convertView.findViewById(R.id.station_parcel_goodsType);
            holder.time = (TextView) convertView.findViewById(R.id.station_parcel_time);
            holder.checked = (CheckBox) convertView.findViewById(R.id.station_parcel_checkbox);
            holder.payCount = (TextView) convertView.findViewById(R.id.parcel_pay_count);
            holder.sendCode = (TextView) convertView.findViewById(R.id.station_parcel_sendcode);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final StationParcelBean item = getItem(position);
        holder.payCount.setVisibility(View.GONE);
        // 根据支付数来显示item
        if (unPayCount == 0) {
            if (position == 0) {
                holder.payCount.setVisibility(View.VISIBLE);
                holder.payCount.setText("已支付" + payedCount + "个");
            }
            holder.checked.setVisibility(View.VISIBLE);
        } else {
            holder.checked.setVisibility(View.GONE);
            if (position == 0) {
                holder.payCount.setVisibility(View.VISIBLE);
                holder.payCount.setText("未支付" + unPayCount + "个");
            } else if (position == unPayCount && payedCount > 0) {
                holder.payCount.setVisibility(View.VISIBLE);
                holder.payCount.setText("已支付" + payedCount + "个");
            }
        }
        // load image
        if (!Str.isEmpty(item.getParcelImgUrl())) {
            showImage(item.getParcelImgUrl(), holder.goodsImg, null);
        }
        if (TextUtils.isEmpty(item.getBagCode()) || "null".equals(item.getBagCode())) {
            holder.bagCode.setText("投递编号: " + "立即绑定");
            //没有绑定袋子，就去扫描绑定
            holder.bagCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((StationParcelListActivity) context).setBindParcelId(item.getParcelId() + "");
                    Intent intent = new Intent();
                    intent.setClass(context, AppCaptureActivity.class);
                    context.startActivityForResult(intent, 103);
                }
            });
        } else {
            holder.bagCode.setText("投递编号: " + item.getBagCode());
        }
        holder.sendCode.setText("寄件码: " + item.getCheckCode());//寄件码
        holder.goodsType.setText("物品类型: " + item.getCategory());
        holder.time.setText("到店时间: " + item.getPostTime());
        if (checkedIds.contains(item.getParcelId())) {
            holder.checked.setChecked(true);
        } else {
            holder.checked.setChecked(false);
        }
        holder.checked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(item.getParcelId());
                ((StationParcelListActivity) context).updateCount();
            }
        });
        return convertView;
    }

    private class ViewHolder {
        ImageView goodsImg;
        TextView bagCode;
        TextView payCount;
        TextView goodsType; // 物品类型
        TextView time;
        CheckBox checked;
        TextView sendCode;//寄件码
    }

    public HashSet<Integer> getCheckIds() {
        if (checkedIds == null) {
            checkedIds = new HashSet<Integer>();
        }
        return checkedIds;
    }

}
